import 'package:flutter/material.dart';
import 'dart:math' as math;

/// 水墨风格背景组件
class InkStyleBackground extends StatefulWidget {
  final Widget? child;
  final Color baseColor;
  final double opacity;
  
  const InkStyleBackground({
    super.key,
    this.child,
    this.baseColor = const Color(0xFF2C3E50),
    this.opacity = 0.1,
  });

  @override
  State<InkStyleBackground> createState() => _InkStyleBackgroundState();
}

class _InkStyleBackgroundState extends State<InkStyleBackground>
    with TickerProviderStateMixin {
  late AnimationController _controller1;
  late AnimationController _controller2;
  late AnimationController _controller3;
  
  @override
  void initState() {
    super.initState();
    
    // 创建多个动画控制器，实现不同速度的水墨流动效果
    _controller1 = AnimationController(
      duration: const Duration(seconds: 20),
      vsync: this,
    )..repeat();
    
    _controller2 = AnimationController(
      duration: const Duration(seconds: 25),
      vsync: this,
    )..repeat();
    
    _controller3 = AnimationController(
      duration: const Duration(seconds: 30),
      vsync: this,
    )..repeat();
  }
  
  @override
  void dispose() {
    _controller1.dispose();
    _controller2.dispose();
    _controller3.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      decoration: _buildGradientBackground(),
      child: Stack(
        children: [
          // 水墨层1
          AnimatedBuilder(
            animation: _controller1,
            builder: (context, child) {
              return CustomPaint(
                painter: InkPainter(
                  animation: _controller1,
                  color: widget.baseColor.withOpacity(widget.opacity),
                  seed: 1,
                ),
                size: Size.infinite,
              );
            },
          ),
          // 水墨层2
          AnimatedBuilder(
            animation: _controller2,
            builder: (context, child) {
              return CustomPaint(
                painter: InkPainter(
                  animation: _controller2,
                  color: widget.baseColor.withOpacity(widget.opacity * 0.8),
                  seed: 2,
                ),
                size: Size.infinite,
              );
            },
          ),
          // 水墨层3
          AnimatedBuilder(
            animation: _controller3,
            builder: (context, child) {
              return CustomPaint(
                painter: InkPainter(
                  animation: _controller3,
                  color: widget.baseColor.withOpacity(widget.opacity * 0.6),
                  seed: 3,
                ),
                size: Size.infinite,
              );
            },
          ),
          // 子组件
          if (widget.child != null) widget.child!,
        ],
      ),
    );
  }
  
  /// 构建渐变背景
  BoxDecoration _buildGradientBackground() {
    return const BoxDecoration(
      gradient: LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
        colors: [
          Color(0xFFF8F9FA),  // 浅灰白
          Color(0xFFE9ECEF),  // 中灰白
          Color(0xFFDEE2E6),  // 深一点的灰白
          Color(0xFFCED4DA),  // 更深的灰白
        ],
        stops: [0.0, 0.3, 0.7, 1.0],
      ),
    );
  }
}

/// 水墨绘制器
class InkPainter extends CustomPainter {
  final Animation<double> animation;
  final Color color;
  final int seed;
  
  InkPainter({
    required this.animation,
    required this.color,
    required this.seed,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;
    
    final random = math.Random(seed);
    
    // 绘制多个水墨点
    for (int i = 0; i < 8; i++) {
      _drawInkBlot(canvas, size, paint, random, i);
    }
  }
  
  /// 绘制单个水墨点
  void _drawInkBlot(Canvas canvas, Size size, Paint paint, math.Random random, int index) {
    final baseX = size.width * (0.1 + 0.8 * random.nextDouble());
    final baseY = size.height * (0.1 + 0.8 * random.nextDouble());
    
    // 添加动画偏移
    final animOffset = animation.value * 2 * math.pi;
    final x = baseX + math.sin(animOffset + index * 0.5) * 30;
    final y = baseY + math.cos(animOffset + index * 0.7) * 20;
    
    // 绘制主体水墨点
    final mainRadius = 20 + random.nextDouble() * 40;
    canvas.drawCircle(
      Offset(x, y),
      mainRadius,
      paint..color = color,
    );
    
    // 绘制周围的小水墨点
    for (int j = 0; j < 5; j++) {
      final angle = (j / 5) * 2 * math.pi + animOffset;
      final distance = mainRadius * (0.5 + random.nextDouble() * 0.8);
      final smallX = x + math.cos(angle) * distance;
      final smallY = y + math.sin(angle) * distance;
      final smallRadius = random.nextDouble() * 15 + 5;
      
      canvas.drawCircle(
        Offset(smallX, smallY),
        smallRadius,
        paint..color = color.withOpacity(color.opacity * 0.6),
      );
    }
    
    // 绘制水墨扩散效果
    for (int k = 0; k < 3; k++) {
      final diffuseRadius = mainRadius + (k + 1) * 20;
      final diffuseOpacity = color.opacity * (0.3 - k * 0.1);
      
      if (diffuseOpacity > 0) {
        canvas.drawCircle(
          Offset(x, y),
          diffuseRadius,
          paint..color = color.withOpacity(diffuseOpacity),
        );
      }
    }
  }

  @override
  bool shouldRepaint(InkPainter oldDelegate) {
    return animation != oldDelegate.animation;
  }
}

/// 水墨装饰性元素
class InkDecorationWidget extends StatelessWidget {
  final double size;
  final Color color;
  final double opacity;
  
  const InkDecorationWidget({
    super.key,
    this.size = 100,
    this.color = const Color(0xFF2C3E50),
    this.opacity = 0.1,
  });

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: _InkDecorationPainter(
        color: color.withOpacity(opacity),
      ),
      size: Size(size, size),
    );
  }
}

class _InkDecorationPainter extends CustomPainter {
  final Color color;
  
  _InkDecorationPainter({required this.color});

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;
    
    final center = Offset(size.width / 2, size.height / 2);
    
    // 绘制中心点
    canvas.drawCircle(center, size.width * 0.2, paint);
    
    // 绘制周围的小点
    final random = math.Random(42);
    for (int i = 0; i < 6; i++) {
      final angle = (i / 6) * 2 * math.pi;
      final distance = size.width * (0.3 + random.nextDouble() * 0.2);
      final x = center.dx + math.cos(angle) * distance;
      final y = center.dy + math.sin(angle) * distance;
      final radius = size.width * (0.05 + random.nextDouble() * 0.1);
      
      canvas.drawCircle(
        Offset(x, y),
        radius,
        paint..color = color.withOpacity(color.opacity * 0.7),
      );
    }
  }

  @override
  bool shouldRepaint(_InkDecorationPainter oldDelegate) {
    return color != oldDelegate.color;
  }
}
